package com.yyge.controller.admin.shoppingcart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.shoppingcart.req.ShoppingCartReq;
import com.yyge.entity.ShoppingCart;
import com.yyge.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/api/merchandise")
public class ShoppingCartController extends BaseController {
    // 注入业务逻辑层
    private ShoppingCartService service;

    @Autowired/*自动装配*/
    public void setService(ShoppingCartService shoppingcartService){
        this.service=shoppingcartService;
    }

    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<ShoppingCart>> get(ShoppingCartReq req) throws Exception  {
         // 将对象进行转换过滤无效条件
        QueryWrapper queryWrapper = this.getWrapper(req);
        PageResp pageResp =this.page(req,pageInfo ->service.page(pageInfo,queryWrapper));
        return R.okHasData(pageResp);
    }


    @PostMapping/* POST(CREATE): 新增&登陆 */
    public R post(@RequestBody ShoppingCart shoppingcart){
        Boolean i=service.save(shoppingcart);
        return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody ShoppingCart shoppingcart){
        Boolean i=service.updateById(shoppingcart);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody ShoppingCart shoppingcart){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

